<?php

function seo_url_rewrite($route, $args) {
	global $CATEGORIES;
	
	static $find = array('{parent_category_dir}', '{category_dir}', '{seo_url}', '{product_id}', '{category_id}');
	$category_seo_rule = C('category_seo_rule');
	$product_seo_rule = C('product_seo_rule');

	//product & category
	if ($route == 'product/product' || $route == 'product/category') {
		parse_str($args, $url_data);
		$seo_url = $parent_category_dir = $category_dir = '';
		$category_id = $product_id = 0;
		if (isset($url_data['category_id'])) {
			$category_id = $url_data['category_id'];
			unset($url_data['category_id']);
		}
		if(isset($CATEGORIES[$category_id])) {
			$seo_url = $category_dir = $CATEGORIES[$category_id]['url'];
			$paths = explode(',', $CATEGORIES[$category_id]['path']);
			array_pop($paths);
			foreach($paths as $path) {
				$parent_category_dir .= $CATEGORIES[$path]['url'] ? $CATEGORIES[$path]['url'] . '/' : '';
			}
			$parent_category_dir = trim($parent_category_dir, '/');
		}
		if ($route == 'product/product') {
			if (isset($url_data['seo_url'])) {
				$seo_url = $url_data['seo_url'];
				unset($url_data['seo_url']);
			}
			else $seo_url = '';
			$product_id = $url_data['product_id'];
			unset($url_data['product_id']);
			$replace = array(
				$parent_category_dir,
				$category_dir,
				$seo_url ? $seo_url : 'product',
				$product_id,
				'',
			);
			$url = str_replace($find, $replace, $product_seo_rule);
			$url = str_replace('//', '/', $url);
		}
		else {
			$replace = array(
				$parent_category_dir,
				'',
				$seo_url ? $seo_url : 'category',
				'',
				$category_id
			);
			$url = str_replace($find, $replace, $category_seo_rule);
		}
		if (!empty($url_data)) {
			$url .= "?" . http_build_query($url_data);
		}
		return ltrim(str_replace('%7Bpage%7D', '{page}', $url), '/');
	}	
	else {
		parse_str($args, $url_data);
		//article
		if ($route == 'article/category') {
			$url = 'article/' . (isset($url_data['category_id']) ? $url_data['category_id'] . '/' : '');
			unset($url_data['category_id']);
			if (!empty($url_data)) {
				$url .= "?" . http_build_query($url_data);
			}
		}
		elseif ($route == 'article/index') {
			$url = 'article-' . $url_data['article_id'] . '.html';
		}
		elseif ($route == 'product/topic') {
			$url = 'topic/' . $url_data['topic_id'] . '.html';
		}
		//manufacturer
		elseif ($route == 'product/manufacturer/info') {
			$url = $route . '/' . $url_data['manufacturer_id'];
			unset($url_data['manufacturer_id']);
			if (!empty($url_data)) {
				$url .= "?" . http_build_query($url_data);
			}
		}
		elseif ($route == 'page/index') {
			if (!empty($url_data['seo_url'])) $url = 'page/' . $url_data['seo_url'] . '.html';
			else $url = 'page/' . $url_data['page_id'] . '.html';
		}
		//others
		else {
			$url = ($route == '/') ? '' : $route;
			if ($args) {
				$url .= '?' . trim($args, '&');
			}
		}
		return str_replace('%7Bpage%7D', '{page}', $url);
	}
}